package com.roger.bcr.test;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.util.Log;

import com.roger.uia.helper.UiBase;
import com.roger.uia.helper.manager.Package;
import com.roger.uia.helper.util.FileUtils;
import com.roger.uia.helper.util.PicUtils;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class Main extends UiBase {
    private static final String TAG = "BCR Test";
    private String result = filePath + "BCR/result.txt";

    @BeforeClass
    public static void beforeClass() throws Throwable{
        Package.pac().grantAllPerms(InstrumentationRegistry.getTargetContext().getPackageName());
    }

    @Before
    public void setUp() {
        Log.d(TAG, "-- set up --");
        FileUtils.deleteFile(result);
    }

    @After
    public void tearDown() {
        Log.d(TAG, "-- tear down --");
    }

    @AfterClass
    public static void afterClass() throws Throwable{

    }

    @Test
    public void main(){
        Bundle para= InstrumentationRegistry.getArguments();
        String wake = para.getString("wake");
        String sleep = para.getString("sleep");
        try {
            if(mDevice.isScreenOn()) mDevice.sleep();
            SystemClock.sleep(waitTime * Integer.parseInt(sleep));

            if(!mDevice.isScreenOn()) mDevice.wakeUp();
            SystemClock.sleep(waitTime * Integer.parseInt(wake) - 1000);
            mDevice.pressHome();
            SystemClock.sleep(waitTime);
            mDevice.pressHome();
            if(!mDevice.isScreenOn()) mDevice.wakeUp();
            PicUtils.pic().takeScreenshot(filePath + "bcr.png");
            if(mDevice.getCurrentPackageName().equals(mDevice.getLauncherPackageName())){
                Log.d(TAG,"success");
                FileUtils.writeFile(result,"Success");
            }else{
                Log.d(TAG,"fail");
                FileUtils.writeFile(result,"Fail");
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Test
    public void debug(){
        Log.d(TAG,result);
        Log.d(TAG,mDevice.getCurrentPackageName());
        Log.d(TAG,mDevice.getLauncherPackageName());
        Log.d(TAG,mDevice.getCurrentActivityName());
    }

}
